package com.ht.aems.fesmodel;

import com.ht.aems.fesmodel.data.AnalogPoint;
import com.ht.aems.fesmodel.data.DiscretePoint;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2007-1-23
 *         Time: 13:54:51
 */
public class FesProperties {
    public static final int CONN_MODE_TCP_SERVER = 0;
    public static final int CONN_MODE_TCP_CLIENT = 1;
    public static final int CONN_MODE_UDP = 2;

    public static final String FES_TITLE = "fes.title";
    public static final String FES_POINT_MANAGER_TITLE = "fes.pointmanager.title";
    public static final String FES_SERVER_PORT = "fes.server.port";
    public static final String FES_SERVER_IP = "fes.server.ip";
    public static final String FES_PERSISTENCE_TYPE = "fes.persistence.type";
    public static final String FES_SERVER_TCP_PORT = "fes.server.tcp.pot";
    public static final String FES_DATASERVER_PORT = "fes.server.dataserver.tcp.port";
    public static final String FES_TCP_SERVER_CLASS = "fes.tcpserver.class";
    public static final String FES_MSG_FILE_PATH = "fes.fesmsg.filepath";

    public static final String FES_DB_NAME = "fes.fesdb.name";
    public static final String FES_HIS_DB_NAME = "fes.hisdb.name";

    private static Map<Integer, String> deviceTypes = new HashMap<Integer, String>();
    private static Map<Integer, String> measureTypes = new HashMap<Integer, String>();

    private static Map<Integer, Integer> protocolConnMode = new HashMap<Integer, Integer>();
    private static Map<Integer, String> protocolNames = new HashMap<Integer, String>();
    private static Map<Integer, String> protocolClassNames = new HashMap<Integer, String>();

    private static Map<Integer, String> rtuNames = new HashMap<Integer, String>();
    private static Map<String, Integer> rtuName2id = new HashMap<String, Integer>();
    private static Map<Integer, AnalogPoint[]> defaultAnalogPoints = new HashMap<Integer, AnalogPoint[]>();
    private static Map<Integer, DiscretePoint[]> defaultDiscretePoints = new HashMap<Integer, DiscretePoint[]>();

    private static ResourceBundle resource;
    private static List<String> protocolNameList = new ArrayList<String>();

    public static void readConf(String file) {
        resource = ResourceBundle.getBundle(file);
    }

    static {
        ResourceBundle rb = ResourceBundle.getBundle("config.devicetype");
        Enumeration<String> keys = rb.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            deviceTypes.put(Integer.parseInt(key), rb.getString(key));
        }
        rb = ResourceBundle.getBundle("config.measuretype");
        keys = rb.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            measureTypes.put(Integer.parseInt(key), rb.getString(key));
        }

        rb = ResourceBundle.getBundle("config.pointsetting");
        int totalNum = Integer.parseInt(rb.getString("rtu.total.num"));
        for (int i = 1; i <= totalNum; i++) {
            String name = rb.getString("rtu" + i + ".name");
            rtuName2id.put(name, i);
            rtuNames.put(i, name);
            //rtuTypes.put(i, Integer.parseInt(rb.getString("rtu" + i + ".type")));
            String protocolName = rb.getString("rtu" + i + ".protocol.name");
            protocolNames.put(i, protocolName);
            if(!protocolNameList.contains(protocolName))
                protocolNameList.add(protocolName);
            protocolClassNames.put(i, rb.getString("rtu" + i + ".protocol.handler"));
            protocolConnMode.put(i, Integer.parseInt(rb.getString("rtu" + i + ".protocol.connmode")));
            int analogPointName = Integer.parseInt(rb.getString("rtu" + i + ".analog.num"));
            int discretePointName = Integer.parseInt(rb.getString("rtu" + i + ".discrete.num"));
            AnalogPoint[] analogPoints = new AnalogPoint[analogPointName];
            DiscretePoint[] discretePoints = new DiscretePoint[discretePointName];
            for (int j = 1; j <= analogPointName; j++) {
                analogPoints[j - 1] = new AnalogPoint();
                analogPoints[j - 1].setPointNum(Integer.parseInt(rb.getString("rtu" + i + ".analog" + j + ".pointnum")));
                analogPoints[j - 1].setName(rb.getString("rtu" + i + ".analog" + j + ".name"));
                analogPoints[j - 1].setScale(Double.parseDouble(rb.getString("rtu" + i + ".analog" + j + ".scale")));
                analogPoints[j - 1].setOffset(Double.parseDouble(rb.getString("rtu" + i + ".analog" + j + ".offset")));
                String limit = rb.getString("rtu" + i + ".analog" + j + ".limit");
                String[] limits = limit.split(";");
                String[] lowerAndUpper = limits[0].split(",");
                analogPoints[j - 1].setLowerLimit1(Double.parseDouble(lowerAndUpper[0]));
                analogPoints[j - 1].setUpperLimit1(Double.parseDouble(lowerAndUpper[1]));
                lowerAndUpper = limits[1].split(",");
                analogPoints[j - 1].setLowerLimit2(Double.parseDouble(lowerAndUpper[0]));
                analogPoints[j - 1].setUpperLimit2(Double.parseDouble(lowerAndUpper[1]));
                lowerAndUpper = limits[2].split(",");
                analogPoints[j - 1].setLowerLimit3(Double.parseDouble(lowerAndUpper[0]));
                analogPoints[j - 1].setUpperLimit3(Double.parseDouble(lowerAndUpper[1]));
            }
            for (int j = 1; j <= discretePointName; j++) {
                discretePoints[j - 1] = new DiscretePoint();
                discretePoints[j - 1].setName(rb.getString("rtu" + i + ".discrete" + j + ".name"));
                discretePoints[j - 1].setPointNum(Integer.parseInt(rb.getString("rtu" + i + ".discrete" + j + ".pointnum")));
            }
            defaultAnalogPoints.put(i, analogPoints);
            defaultDiscretePoints.put(i, discretePoints);
        }
    }

    public static String getProperty(String key) {
        if(resource != null)
            return resource.getString(key);
        else
            return null;
    }

    public static Map<Integer, String> getDeviceTypes() {
        return deviceTypes;
    }

    public static Map<Integer, String> getMeasureTypes() {
        return measureTypes;
    }

    public static Map<Integer, String> getProtocolClassNames() {
        return protocolClassNames;
    }

    public static Map<Integer, Integer> getProtocolConnMode() {
        return protocolConnMode;
    }

    public static Map<Integer, String> getProtocolNames() {
        return protocolNames;
    }

    public static Map<Integer, String> getRtuNames() {
        return rtuNames;
    }

    public static Map<String, Integer> getRtuName2id() {
        return rtuName2id;
    }

    public static Map<Integer, AnalogPoint[]> getDefaultAnalogPoints() {
        return defaultAnalogPoints;
    }

    public static Map<Integer, DiscretePoint[]> getDefaultDiscretePoints() {
        return defaultDiscretePoints;
    }

    public static List<String> getProtocolNameList() {
        return protocolNameList;
    }
}
